django.db.utils.IntegrityError: The row in table 'first | 您所在的位置:网站首页 › corresponding value › django.db.utils.IntegrityError: The row in table 'first |
昨天用django写网站的时候,用了一个外键后,一直报下面的错。 django.db.utils.IntegrityError: The row in table 'first_blog_blog' with primary key '1' has an invalid foreign key: first_blog_blog.blog_type_id contains a value 'python' that does not have a corresponding value in first_blog_blogtype.id.代码没有错误。 可能的原因分析: 数据库冲突。 而这个错误使用python manage.py makemigrations和python manage.py migrate来更新这个数据库是行不通的。 解决方案:删库~ 写在前面: 因为我的网站是本地网站,还没有部署,没有其它从网络下载的信息,所以删除数据库后,重新创建数据库会把本地网站的东西重新写入。但其它一切手动操作数据库存入的都会被删除,不可找回。比如创建的超级管理员。 1、备份数据库。关闭执行程序后,将数据库改个名字(在后面加个123之类的),像这样
4、执行python manage.py migrate 5、重新创建超级管理员:执行python manage.py createsuperuser 6、ok了 由于本人比较懒,数据库里也没有什么信息,所以选择了建立数据库副本,然后删库~ 最后,成功解决了问题。 但毕竟删库是很危险的事情,后面会学习数据库的基本操作来解决这种数据冲突的问题。 |
CopyRight 2018-2019 实验室设备网 版权所有 |